﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.Infra.Service.Security.Authorization;
using Sinacor.AuditLogging.LogEntry;

namespace Sinacor.Infra.Service.AuditLogging
{

    public class LogSender
    {
        /// <summary>
        /// Método de envio de logs de auditoria de serviços WCF do Sinacor.
        /// </summary>
        public static void Send(SinacorBaseLogEntry logEntry)
        {
            if (logEntry == null)
                throw new ArgumentNullException("logEntry");

            FillDefaultValues(logEntry);
            PSE.AuditLogging.Client.LogSender.Send(logEntry);
        }

        private static void FillDefaultValues(SinacorBaseLogEntry logEntry)
        {
            //caso o contexto de autenticacao nao exista, disparar error específico
            if (AuthorizationContext.Current == null)
                throw new Exception("Unable to fill logEntry internal values. AuthorizationContext context is not set.");

            logEntry.UserId = AuthorizationContext.Current.UserId;
            logEntry.CompanyId = AuthorizationContext.Current.CompanyId;
            logEntry.ModuleId = Convert.ToString(AuthorizationContext.Current.ModuleId);
        }
    }
}
